<template>
  <div class="department">
    <div class="listContainer" :key="index" v-for="(item,index) in departments">
      <div class="tools">
        <a-button @click="addNew(index)" type="primary">增加</a-button>
        <a-button @click="edit(index)">修改</a-button>
        <a-popconfirm title="选中权限和其子权限都将被删除?" @confirm="del(index)" okText="确认" cancelText="取消">
          <a-button type="danger">删除</a-button>
        </a-popconfirm>
      </div>
      <div class="list">
        <div v-if="item.length == 0" class="noData">暂无数据</div>
        <div
          @click="itemClick(subItem)"
          :class="['item',subItem.selected?'itemSelected':'']"
          :key="subIndex"
          v-for="(subItem,subIndex) in item"
        >{{subItem.department_name}}</div>
      </div>
    </div>
    <editor ref="editor"></editor>
  </div>
</template>
<script>
import editor from "./department/editor";
export default {
  components: {
    editor
  },
  data() {
    return {
      showModel: false,
      curLevel: 0,
      departments: [[]]
    };
  },
  created() {
    this.getData(0, this.$emptyId); //todo 找一找"00000000-0000-0000-0000-000000000000"
  },
  methods: {
    async itemClick(subItem) {
      if (subItem.selected) return;
      this.departments[subItem.level_num].forEach(v => (v.selected = false));
      subItem.selected = true;
      this.getData(subItem.level_num + 1, subItem.id);
    },
    async getData(level_num, parent_id) {
      let result = await this.$post("/employee/department/getByParentId", {
        parent_id
      });
      if (result.error) return;
      if (level_num == 0) {
        this.departments = [result.data];
        return;
      }
      if (level_num < this.departments.length) {
        let delCount = this.departments.length - level_num;
        this.departments.splice(level_num, delCount, result.data);
        return;
      }
      this.departments.push(result.data);
    },
    async del(index) {
      let subIndex = this.departments[index].findIndex(v => v.selected == true);
      if (subIndex < 0) {
        this.$notification["error"]({
          message: "请选择要删除的项"
        });
        return;
      }
      let item = this.departments[index][subIndex];
      let result = await this.$post("/system/department/del", {
        id: item.id
      });
      if (result.error) return;
      if (item.selected) {
        this.departments.splice(index + 1);
      }
      this.departments[index].splice(subIndex, 1);
    },
    addNew(index) {
      this.curLevel = index;
      this.showModel = true;
    },
    edit(index) {
      let item = this.departments[index].find(v => v.selected == true);
      if (!item) {
        this.$notification["error"]({
          message: "请选择要删除的项"
        });
        return;
      }
      this.curLevel = index;
      this.showModel = true;
      this.$nextTick(_ => {
        let obj = {
          department_name: item.department_name,
          department_type: item.department_type,
          order_num: item.order_num,
          department_content: item.department_content,
          extra: item.extra,
          id: item.id
        };
        this.$refs.editor.form.setFieldsValue(obj);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
</style>

<style lang="scss" scoped>
.department {
  height: 100%;
  display: flex;
  .item {
    line-height: 42px;
    text-align: center;
    border-bottom: 1px solid #ddd;
    cursor: pointer;
  }
  .item:hover {
    background: #e6f7ff;
  }
  .itemSelected {
    background: #e6f7ff;
    border-bottom: 2px solid #1890ff;
  }
  .noData {
    padding-top: 68px;
    font-size: 28px;
    font-weight: bold;
    color: #ddd;
    text-align: center;
  }
  .listContainer {
    display: flex;
    flex-direction: column;
    margin-right: 18px;
  }
  .tools {
    height: 40px;
  }
  .tools button {
    margin-right: 8px;
  }
  .list {
    width: 280px;
    border: 1px solid #ddd;
    flex: 1;
    margin-right: 16px;
    border-radius: 3px;
    overflow-y: auto;
    overflow-x: hidden;
  }
}
</style>